<template>
  <!-- 添加群 -->
  <div class="add-group">
    <div class="action">
      <div class="item flex-center" @click="onCancel">
        <span>取消</span>
      </div>
      <div class="item send flex-center" @click="onDelete">
        <span>完成 <span v-if="deleteCount > 0">({{ deleteCount }})</span></span>
      </div>
    </div>
    <div class="member-list-wrapper">
      <ul class="member-list">
        <li
          class="list-item flexbox justify-around align-center"
          v-for="(item, i) of memberList"
          :key="i"
          @click="onMemberClicked(item, i)"
        >
          <div class="select">
            <div class="radio flex-center" :class="{'selected': item.selected}">
              <img v-if="item.selected" src="@/assets/icons/check.svg" alt/>
            </div>
          </div>
          <div class="head">
            <img :src="item.headUrl" alt />
          </div>
          <div class="name ellipsis">{{ item.nickname }}</div>
          <div class="info">

          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script lang="ts">
import { Action, Mutation, Getter, namespace } from 'vuex-class';
import { Component, Vue, Prop } from "vue-property-decorator";
import MessageMixin from '@/mixins';
import { MsgTypeKV } from '@/api/socket/imClient';
import { Webim } from '@/utils/types';
import Cookie from 'js-cookie';
declare const $Vue: Vue;
declare const webim: Webim;
const chatModule = namespace('chatModule');
const userModule = namespace('userModule');
@Component<DeleteGroup>({
  mixins: [MessageMixin],
})
export default class DeleteGroup extends Vue {

  @Prop({})
  card: any;

  @userModule.State("groupMembers") groupMembers: any;
  @userModule.Action("saveCurrentUserFN") saveCurrentUserFN: any;
  @userModule.Action("saveGroupMembersFN") saveGroupMembersFN: any;

  private groupNick: string = '';

  private get deleteCount() {
    return this.memberList.filter((item) => {
      return item.selected
    }).length
  }
  // private memberList: Array<any> = [];

  private get memberList(): Array<any> {
    let userId = localStorage.getItem('userId');
    return this.groupMembers.filter((item) => {
      return item.userId !=  userId && item.status == 3
    });
  }

  protected mounted() {
    // this.memberList = this.groupMembers.slice();
    // console.warn(this.groupMembers);
    
  }



  private onCancel() {
    this.$emit('hide');
  }

  private onDelete() {

    if (this.deleteCount > 0) {
      this.$createDialog({
        type: 'confirm',
        icon: 'cubeic-alert',
        title: '确认删除群成员?',
        confirmBtn: {
          text: '确定',
          active: true,
          disabled: false,
          href: 'javascript:;'
        },
        cancelBtn: {
          text: '取消',
          active: false,
          disabled: false,
          href: 'javascript:;'
        },
        onConfirm: () => {
          this.deleteMembers();
        },
        onCancel: () => {
        }
      }).show()
    }
  }

  /**
   * 遍历删除
   */
  private deleteMembers() {
    let accountList: Array<any> = [];
    this.memberList.forEach((item) => {
      if (item.selected) {
        accountList.push(item.imAccount)
      }
    });

    this.sendMessage(MsgTypeKV.kickOut, {
      imUserList: accountList,
    });

  }

    /**
   * 跳转到用户信息
   */
  private onMemberClicked(item: any, i: number) {
    // item.selected = !item.selected;

    this.$set(this.memberList[i], 'selected', !item.selected);
  }

    /**
   * 获取加群列表
   */
  private $getMemberList(data: any) {
    if (data.result === 0) {
      this.saveGroupMembersFN(data.infoList);
    } else {
      this.$createToast({
        txt: data.message,
        type: "warn",
        time: 3000
      }).show();
    }
  }
}
</script>

<style lang="stylus" scoped>
.add-group
  width 100%
  height 100%
  background #fff
  padding 2px 
  box-sizing border-box
  position fixed 
  top 0 
  left 0
  .icon
    flex-basis 10%
  
.action
  height 40px
  width 100%
  display flex
  justify-content space-between
  align-items center
  padding 4px
  box-sizing border-box
  background #f1f1f1
  .item 
    min-width 64px
    height 100% 
    line-height 100% 
    &.send 
      background rgb(87, 189, 109)
      border-radius 4px
      color white

.member-list-wrapper
  width 100%
  .member-list
    padding 2px 6px
    .list-item 
      height 48px
      border-bottom 0.2px solid #f1f1f1
      .select
        flex-basis 10%
        width 10%
        .radio
          width 18px
          height 18px
          border-radius 50% 
          border 0.8px solid #a2a2a2
          img 
            width 18px 
            height 18px 
          &.selected
            background rgb(87, 189, 109)
            border-color rgb(87, 189, 109)
      .head
        flex-basis 10%
        width 10%
        img 
          width 36px 
          height 36px 
          border-radius 4px
      .name
        flex-basis 70%
        width 70%
      .info
        flex-basis 10%
        width 10%
</style>

